<?php
//微信用户相关

namespace App\Http\Controllers;

use App\Models\DealModel;
use App\Models\OperateModel;
use App\Models\RiskCheckModel;
use App\Repositories\OperateRepository;
use Illuminate\Http\Request;
use App\Repositories\DealRepository;

class DealController extends BaseController
{

    /**
     * @param Request $request
     * @param DealRepository $dealRepository
     * @return \Illuminate\Http\JsonResponse
     */
    public function list (Request $request, DealRepository $dealRepository)
    {
        $params = $request->all();
        $list = @$dealRepository->getList($params);
        return $this->success($list);
    }

    /**
     * @param Request $request
     * @param DealRepository $dealRepository
     * @return \Illuminate\Http\JsonResponse
     */
    public function save (Request $request, DealRepository $dealRepository, OperateRepository $operateRepository)
    {
        $allParams = $request->all();
        $dealId = $allParams['id'] ?? '';
        $price = $allParams['price'];
        $isChange = false;
        $shareNumber = $allParams['share_number'];
        if ($dealId) {
            $isChange = @$dealRepository->checkPriceChange($dealId, $price, $shareNumber);
        }
        $riskCheck = @RiskCheckModel::where('stock_code', $allParams['stock_code'])
            ->whereDate('created_at', today())
            ->orderBy('id', 'desc')
            ->first();
        if ($riskCheck) {
            $allParams['risk_check_id'] = $riskCheck['id'];
        }
        $rst = @$dealRepository->save($allParams);
        if ($isChange) {
            // 如果价格有变化，则将操作状态改为未完成
            @$operateRepository->saveOrUpdate('OperateModel', [
                'id' => $allParams['operate_id'],
                'is_finished' => OperateModel::UNFINISHED
            ]);
        }
        if ($rst) {
            return $this->success([]);
        }
        return $this->error(405, '保存失败，请稍后再试');
    }

    /**
     * @param Request $request
     * @param DealRepository $dealRepository
     * @return \Illuminate\Http\JsonResponse
     */
    public function getDealInfo (Request $request, DealRepository $dealRepository)
    {
        $id = $request->get('id');
        $data = @$dealRepository->getDealInfo($id);
        if ($data) {
            return $this->success($data);
        } else {
            return $this->error(401, '信息获取失败，请稍后再试');
        }
    }

    /**
     * @param Request $request
     * @param DealRepository $dealRepository
     * @return \Illuminate\Http\JsonResponse
     */
    public function getFollowInfo (Request $request, DealRepository $dealRepository)
    {
        $id = $request->get('id');
        $data = @$dealRepository->getFollowInfo($id);
        if ($data) {
            return $this->success($data);
        } else {
            return $this->error(401, '信息获取失败，请稍后再试');
        }
    }

    /**
     * @param Request $request
     * @param DealRepository $dealRepository
     * @return \Illuminate\Http\JsonResponse
     */
    public function scheckPriceChangeave (Request $request, DealRepository $dealRepository)
    {
        $price = $request->get('price');
        $dealId = $request->get('deal_id');
        $shareNumber = $request->get('share_number');
        $isChange = @$dealRepository->checkPriceChange($dealId, $price, $shareNumber);
        return $this->success([
            'is_change' => $isChange
        ]);
    }
}
